package com.haotunet.app.goldtreasure.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.SellRecordAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.SellRecord;
import com.haotunet.app.goldtreasure.model.SellRecordModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

import java.util.List;

public class SellRecordFragment extends BaseFragment {
	private PullableListView mLvSellRecord;
	private View view;
	private LinearLayout mNoRecord;
	private PullToRefreshLayout mfreshLayout;
	private boolean isPrepared;
	private boolean mHasLoadedOnce;
	private int pageNum = 20;
	private final int GET_SELL_RECORD_SUCCESS = 2;
	private final int LOAD_MORE_SELL_RECORD_SUCCESS = 3;
	private SellRecordAdapter sellRecordAdapter;
	private Handler handle = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

			case GET_SELL_RECORD_SUCCESS:
				SellRecordModel model = (SellRecordModel) msg.obj;
				List<SellRecord> sellList = model.getItems();
				if (sellList.size() == 0) {
					mNoRecord.setVisibility(View.VISIBLE);
					return;
				}
				sellRecordAdapter = new SellRecordAdapter(getActivity(), sellList);
				sellRecordAdapter.setDatas(sellList);
				mLvSellRecord.setAdapter(sellRecordAdapter);
				break;
			case LOAD_MORE_SELL_RECORD_SUCCESS:
				SellRecordModel moreModel = (SellRecordModel) msg.obj;
				if (moreModel.getItems().size()==0) {
					UIHelper.showToast(getActivity(), getString(R.string.last_page_hint));
				}
				sellRecordAdapter.addDatas(moreModel.getItems());
				break;
			default:
				break;
			}
		}

	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_sell_record, null);
		initView();
		isPrepared = true;
		return view;
	}

	private void getSellRecord(String pagenum, final String action) {

		ActionUtil actionUtil = new ActionUtil(getActivity());
		actionUtil.getSellGoldRecord(pagenum);
		actionUtil.setOnPassBackListener(new OnPassBackListener() {

			@Override
			public void passBack(BaseModel model) {
				if (!isActivityAvailable()) {
					return;
				}
				SellRecordModel sellModel = (SellRecordModel) model;
				if (sellModel == null) {
					UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
					mfreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
					return;
				}
				if (sellModel.getErrorMsg() != null) {
					if (sellModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
						ActivityUtils.forceQuit(getActivity(),getString(R.string.crowding_in_tips_txt));
					} else {
						UIHelper.showToast(getActivity(), sellModel.getErrorMsg());
					}
				} else {
					mHasLoadedOnce = true;
					if (action.equals("refresh") || action.equals("loadmore")) {
						mfreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
					}
					Message msg = new Message();
					if (action.equals("loadmore")) {
						msg.what = LOAD_MORE_SELL_RECORD_SUCCESS;
						pageNum = pageNum + 20;
					} else {
						msg.what = GET_SELL_RECORD_SUCCESS;
					}
					msg.obj = sellModel;
					handle.sendMessage(msg);
				}
			}
		});
	}

	private void initView() {
		mNoRecord = (LinearLayout) view.findViewById(R.id.no_trade_record_layout);
		mfreshLayout = (PullToRefreshLayout) view.findViewById(R.id.id_swipe_ly);
		mLvSellRecord = (PullableListView) view.findViewById(R.id.lv_sell_record);
		RefreshData(0);
	}

	private void RefreshData(final int pagenum) {
		mfreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
				getSellRecord(pagenum + "", "refresh");
				pageNum = 20;
			}

			@Override
			public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
				getSellRecord(pageNum + "", "loadmore");
			}
		});
	}

	@Override
	protected void lazyLoad() {
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}
		getSellRecord("0", "");
	}

}
